package com.casework

class OfferController {
    static scaffold = true

    def create = {
        def caseId = params.remove('caseId')
        def offerInstance = new Offer()
        offerInstance.properties = params
        return [offerInstance: offerInstance, caseId: caseId]
    }

    def save = {
        def offerInstance = new Offer(params)
        def legalCase = LegalCase.get(params.caseId)

        if (offerInstance.save(flush: true)) {
            if (legalCase) {
                legalCase.addToOffers(offerInstance).save(flush:true)
                redirect action:'show', id: legalCase.id, controller: 'legalCase'
                return
            }

            flash.message = "${message(code: 'default.created.message', args: [message(code: 'offer.label', default: 'Offer'), offerInstance.id])}"
            redirect(action: "show", id: offerInstance.id)
        }
        else {
            render(view: "create", model: [offerInstance: offerInstance, caseId: legalCase?.id])
        }
    }
}
